describe("ItemAjaxService", function(){

    var callback,
    fakeServer;

    beforeEach(function(){

        fakeServer = sinon.fakeServer.create();
        callback = sinon.spy();

    });

    afterEach(function(){

        fakeServer.restore();
        callback.reset();

    });

    describe("getItemById", function(){

        it ("should return item by id from server", function(){

            var service = new ItemAjaxService("www.my-service.com","/items"),
                someItem = {
                    "id": "1",
                    "name": "itemName"
                };

            fakeServer.respondWith("GET", "www.my-service.com/items?id=1",
                    [200, { "Content-Type": "application/json" }, JSON.stringify(someItem)]);

            service.getItemById("id=1", callback);

            fakeServer.respond();

            expect(callback).toHaveBeenCalledWith(someItem);

        });

    });

    describe("queryItemsByName", function(){

        it ("should return item which contains query string in its name", function(){

            var service = new ItemAjaxService("www.my-service.com","/items"),
                someItem = {
                    "id": "1",
                    "name": "itemName",
                    "location" : "Ukraine"
                };

            fakeServer.respondWith("GET", "www.my-service.com/items?location=Ukraine",
                    [200, { "Content-Type": "application/json" }, JSON.stringify(someItem)]);

            service.queryItemsByName("location=Ukraine", callback);

            fakeServer.respond();

            expect(callback).toHaveBeenCalledWith(someItem);

        });

    });

    describe("create", function(){

        it ("should create items provided in items array on the server", function(){

            var service = new ItemAjaxService("www.my-service.com","/items"),
                item = [
                    {
                        "name": "itemName_1",
                        "location" : "Ukraine"
                    }
                ];

            fakeServer.respondWith("POST", "www.my-service.com/items",
                    [200, { "Content-Type": "text/html" }, "1"]);


            service.create(item, callback);

            fakeServer.respond();

            expect(callback.callCount).toBe(1);

            expect(callback).toHaveBeenCalledWith("1");

        });

    });

    describe("update", function(){

        it ("should update item", function(){

            var service = new ItemAjaxService("www.my-service.com","/items?id=1"),
                itemsList=[
                    {
                        "name": "itemName_1990",
                        "location" : "Ukraine",
                        "id" : "1"
                    }
                ],
                item = [
                    {
                        "name": "itemName_1",
                        "location" : "Ukraine",
                        "id" : "1"
                    }
                ];

            fakeServer.respondWith("PUT", "www.my-service.com/items?id=1",
                    [200, { "Content-Type": "text/html" }, "1"]);

            service.update(item, callback);

            fakeServer.respond();

            expect(callback.callCount).toBe(1);

            expect(callback).toHaveBeenCalledWith("1");

        });

    });

    describe("remove", function(){

        it ("should remove item", function(){

            var service = new ItemAjaxService("www.my-service.com","/items"),
                itemsList=[
                    {
                        "name": "itemName_1990",
                        "location" : "Ukraine",
                        "id" : "1"
                    },
                    {
                        "name": "itemName_1991",
                        "location" : "Ukraine",
                        "id" : "2"
                    }
                ];

            fakeServer.respondWith("DELETE", "www.my-service.com/items",
                    [200, { "Content-Type": "text/html" }, "1, 2"]);

            service.remove(["1", "2"], callback);

            fakeServer.respond();

            expect(callback.callCount).toBe(2);

            expect(callback).toHaveBeenCalledWith("1, 2");

        });

    });

})
